package com.zhp.farmerclient.controller;


import com.zhp.farmerclient.common.Result;
import com.zhp.farmerclient.entity.FileVO;
import com.zhp.farmerclient.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@CrossOrigin//解决前端后端跨域问题
public class FileController {
    @Autowired
    public FileService fileService;

    @PostMapping("/upload")
    public FileVO upload(MultipartFile file) throws IOException{
        FileVO fileVO = fileService.upload(file);
        return fileVO;
    }
    @PostMapping("/remove")
    public Result remove(@RequestParam String filename){
        return fileService.remove(filename);
    }

    @GetMapping("/downloadFile")
    public Object downloadFile(HttpServletResponse response, String filePath) throws IOException {
        // 清空输出流
        response.reset();
        response.setContentType("application/x-download;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ new String(filePath.getBytes("utf-8"), "utf-8"));
        fileService.download(filePath,response);
        return null;
    }

}
